<?php
include_once "common/cnfg.inc";

function main(&$view)
{/*{{{*/
    $view=array();
    $svcenv=new svcenv();
    //$svcenv=new svcenv(cnfg::$svcenv['default']);
    $session=$svcenv->session();

    $uuid="zhou.arron@gmail.com";
    $view['uuid']=$uuid;
    $pairs=array('key1'=>100, 'key2'=>200, 'key3'=>300);
    $view['pairs']=$pairs;
    $keys=array('key1', 'key2', 'key3');
    $view['keys']=$keys;
    $returl="http://site.com/login.php?action=somework";
    $view['returl']=$returl;

    $create_sid=$session->create($uuid);
    if($create_sid === false)
    {
        if($session->last_error != SOAR_ERR::SESSION_EXIST) 
        {
            $view['create_sid']=$session->last_error;
            return;
        }
        $view['create_sid']='已创建';
    }
    else
    {
        $view['create_sid']=$create_sid;
    }

    $get_sid=$session->get("zhou.arron@gmail.com");
    if($get_sid === false)
    {
        $view['get_sid']=$session->last_error;
        return;
    }
    $view['get_sid']=$get_sid;

    $res=$session->validate();
    if($res == false)
    {
        $view['validate']=$session->last_error;
        return;
    }
    $view['validate']='成功';

    $res=$session->putkey($pairs);
    if($res == false)
    {
        $view['putkey']=$session->last_error;
        return;
    }
    $view['putkey']='成功';

    $res=$session->modkey('key1', -1);
    if($res == false)
    {
        $view['modkey']=$session->last_error;
        return;
    }
    $view['modkey']='成功';

    $vals=$session->getkey($keys);
    if($vals === false)
    {
        $view['getkey_result']=$session->last_error;
        return;
    }
    $view['getkey_result']=$session->last_error;
    $view['getkey']=$vals;

    $res=$session->destroykey('key1');
    if($res == false)
    {
        $view['destroykey']=$session->last_error;
        return;
    }
    $view['destroykey']='成功';

    $res=$session->destroy();
    if($res == false)
    {
        $view['destroy']=$session->last_error;
        return;
    }
    $view['destroy']='成功';

    $returl_enc=$session->encrypt($returl);
    if($returl_enc === false)
    {
        $view['returl_enc']=$session->last_error;
        return;
    }
    $view["returl_enc"]=$returl_enc;

    $returl_plain=$session->decrypt($returl_enc);
    if($returl_plain == false)
    {
        $view['returl_plain']=$session->last_error;
        return;
    }
    $view['returl_plain']='成功';

    $svcenv->close();
}/*}}}*/

main($VIEW);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<?php
echo "创建会话：uuid=".$VIEW['uuid']." create_sid=".$VIEW['create_sid']."<br/>\n";
echo "获取会话：uuid=".$VIEW['uuid']." get_sid=".$VIEW['get_sid']."<br/>\n";
echo "刷新会话：".$VIEW['validate']."<br/>\n";
echo "设置键值：";
foreach($VIEW['pairs'] as $key=>$val)
{
    echo $key."=>".$val." ";
}
echo $VIEW['putkey']."<br/>\n";
echo "改动键值：".$VIEW['modkey']."<br/>\n";
echo "获取键值：";
if($VIEW['getkey_result'] == SOAR_ERR::OK) 
{
    for($i=0; $i<count($VIEW['keys']); ++$i)
    {
        echo $VIEW['keys'][$i]."=>".$VIEW['getkey'][$i]." ";
    }
}
else
{
    echo $VIEW['getkey_result'];
}
echo "<br/>\n";
echo "删除键值：".$VIEW['destroykey']."<br/>\n";
echo "删除会话：".$VIEW['destroy']."<br/>\n";
echo "原文：".$VIEW['returl']."<br/>\n";
echo "加密：".$VIEW['returl_enc']."<br/>\n";
echo "解密：".$VIEW['returl_plain']."<br/>\n";
?>
</body>
</html>
